#include <stdio.h>

#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>

#include "copi.h"
#include "copi_ui.h"
#include <log.h>
#include "lua_docall.h"

int luaopen_winfile(lua_State *L);

static int c_log_debug( lua_State* L )
{
    const char* str = lua_tostring( L, 1 );
    log_debug( "%s", str );
    return 0;
}

static int c_log_warn( lua_State* L )
{
    const char* str = lua_tostring( L, 1 );
    log_warn( "%s", str );
    return 0;
}

static int c_log_error( lua_State* L )
{
    const char* str = lua_tostring( L, 1 );
    log_error( "%s", str );
    return 0;
}

static int c_log_info( lua_State* L )
{
    const char* str = lua_tostring( L, 1 );
    log_info( "%s", str );
    return 0;
}

static int luaopen_log( lua_State* L )
{
    luaL_Reg log [] = { { "info", c_log_info },
                        { "debug", c_log_debug },
                        { "warn", c_log_warn },
                        { "error", c_log_error },
                        { NULL, NULL } };
    luaL_newlib( L, log );

    log_info( "初始化log模块" );

    return 1;
}

static int reload( lua_State* L )
{
    luaL_dostring( L,
                   "_G['copi'] = reload('init'); _G['copi_init'] = copi.init; "
                   "_G['copi_update'] = copi.update" );

    lua_getglobal( L, "copi_init" );
    int rvl = docall( L, 0, 0 );
    if ( rvl != 0 ) {
        log_error( "reload init 失败, %d", rvl );
        log_error( "%s", lua_tostring( L, -1 ) );
    }
    return 0;
}

int main( int argc, char** argv )
{
    log_set_level( 0 );
    log_set_quiet( false );

    FILE* log_fd = fopen( COPI_DEFAULT_LOG_FILE_PATH, "ab" );
    log_add_fp( log_fd, COPI_DEFAULT_LOG_LEVEL );

    log_info( "Opening script module......" );
    lua_State* lua_state = luaL_newstate();
    luaL_openlibs( lua_state );

    luaL_dostring(
        lua_state,
        "package.cpath=package.cpath .. ';./clib/?.dll;./clib/?.so'"
        "function reload(module) package.loaded[module] = nil; return require(module); end;" );

    lua_register( lua_state, "c_log_info", c_log_info );
    lua_register( lua_state, "copi_env_reload", reload );

    lua_getglobal( lua_state, "package" );
    lua_getfield( lua_state, -1, "preload" );

    lua_pushcfunction( lua_state, luaopen_log );
    lua_setfield( lua_state, -2, "log" );

    lua_pushcfunction( lua_state, luaopen_ui );
    lua_setfield( lua_state, -2, "ui" );

  lua_pushcfunction(lua_state, luaopen_winfile);
    lua_setfield( lua_state, -2, "lfs" );

    lua_pop( lua_state, 2 );

    copi_oswindow_open( lua_state );

    // 初始化脚本
    int rvl = luaL_dostring( lua_state,
                             "_G['copi'] = require('init'); _G['copi_init'] = copi.init;"
                             "_G['copi_update'] = copi.update;" );
    if ( rvl != 0 ) {
        log_error( "init 2失败, %d", rvl );
        log_error( "%s", lua_tostring( lua_state, -1 ) );
    }

    // 加载init.lua的初始化函数
    lua_getglobal( lua_state, "copi_init" );
    rvl = docall( lua_state, 0, 0 );
    if ( rvl != 0 ) {
        log_error( "init 失败, %d", rvl );
        log_error( "%s", lua_tostring( lua_state, -1 ) );
    }

    copi_oswindow_loop( lua_state );

    copi_oswindow_close( lua_state );

    lua_close( lua_state );

    log_info( "Log module closeing" );
    fclose( log_fd );

    return 0;
}
